package com.lc.pianostudent.adapter;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.lc.pianostudent.BaseApplication;
import com.lc.pianostudent.R;
import com.lc.pianostudent.activity.ContactDetailActivity;
import com.lc.pianostudent.activity.TeacherDetailActivity;
import com.lc.pianostudent.bean.CircleBean;
import com.lc.pianostudent.bean.CommentsBean;
import com.lc.pianostudent.bean.FindBean;
import com.lc.pianostudent.bean.HomeNewsBean;
import com.lc.pianostudent.bean.Post;
import com.lc.pianostudent.bean.User;
import com.lc.pianostudent.view.CommentListView;
import com.lc.pianostudent.view.SelectPopupWindow;
import com.lc.pianostudent.view.SelectPopupWindowNoPosition;
import com.lc.pianostudent.widget.NineGridLayoutView;
import com.lc.pianostudent.widget.PraiseListView;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.scale.ScaleScreenHelperFactory;
import com.zcx.helper.util.UtilToast;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by Administrator on 2018/6/25.
 */

public class FindAdapter extends RecyclerView.Adapter<FindAdapter.ViewHolder>{


    private Activity context;

    private List<CircleBean.ListBean> list = new ArrayList<>();
    private SelectPopupWindow menuWindow;
    private SelectPopupWindowNoPosition selectPopupWindowNoPosition;
    public FindAdapter(Activity context, List<CircleBean.ListBean> list) {
        this.context = context;
        this.list = list;

    }
    //itemview点击事件
    public interface OnItemClickListener {

        void onItemClick(View view, int position);

    }

    private FindAdapter.OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(FindAdapter.OnItemClickListener listener) {

        this.onItemClickListener = listener;

    }
    //点赞点击事件
    public interface OnItemDianzanClickListener {

        void onItemDianzanClick(View view, int position);

    }

    private FindAdapter.OnItemDianzanClickListener onItemDianzanClickListener;

    public void setOnItemDianzanClickListener(FindAdapter.OnItemDianzanClickListener listener) {

        this.onItemDianzanClickListener = listener;

    }

    //副评论点击事件
    public interface OnItemCommentClickListener {

        void onItemCommentClick(View view, int position ,int comment_posi,List<CommentsBean> comment);

    }

    private FindAdapter.OnItemCommentClickListener onItemCommentClickListener;

    public void setOnItemCommentClickListener(FindAdapter.OnItemCommentClickListener listener) {

        this.onItemCommentClickListener = listener;

    }

    //主评论点击事件
    public interface OnItemZhuCommentClickListener {

        void onItemZhuCommentClick(View view, int position);

    }

    private FindAdapter.OnItemZhuCommentClickListener onItemZhuCommentClickListener;

    public void setOnItemZhuCommentClickListener(FindAdapter.OnItemZhuCommentClickListener listener) {

        this.onItemZhuCommentClickListener = listener;

    }

    //删除状态点击事件
    public interface OnItemDelectClickListener {

        void onItemDelectClick(View view, int zhuangtai_position);

    }

    private FindAdapter.OnItemDelectClickListener onItemDelectClickListener;

    public void setOnItemDelectClickListener(FindAdapter.OnItemDelectClickListener listener) {

        this.onItemDelectClickListener = listener;

    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_find, parent, false);

        ScaleScreenHelperFactory.getInstance().loadViewGroup((ViewGroup) view);

        FindAdapter.ViewHolder viewHolder = new FindAdapter.ViewHolder(view);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {

        if (list.isEmpty()) {
            onBindViewHolder(holder, position);
        }else {
//            昵称
            if((list.get(position).getId()+"").equals(BaseApplication.BasePreferences.readTeacherId())){
                holder.album_user_name_txt.setText(list.get(position).getCname().substring(0,1)+"老师");
            }else {
                holder.album_user_name_txt.setText(list.get(position).getName()+" ( "+list.get(position).getCname()+" )");

            }
//事件
            holder.album_time.setText(list.get(position).getTime());
//            朋友圈内容
            if(!TextUtils.isEmpty(list.get(position).getContent())){
                holder.album_content_txt.setVisibility(View.VISIBLE);
                holder.album_content_txt.setText(list.get(position).getContent());
            }else {
                holder.album_content_txt.setVisibility(View.GONE);
            }

            Glide.with(context).load(list.get(position).getAvatar()).into(holder.album_picture);
//九宫格照片
            holder.layout_nine_grid.setUrlList(list.get(position).getPicarr());
//朋友圈删除按钮是否出现
            if((list.get(position).getId()+"").equals(BaseApplication.BasePreferences.readUID())){
                Log.e("----","visible");
                holder.tv_delete.setVisibility(View.VISIBLE);
            }else {
                Log.e("----","invisible");
                holder.tv_delete.setVisibility(View.GONE);
            }

            if((list.get(position).getUp()+"").equals("1")){
                holder.album_praise_layout.setImageResource(R.mipmap.telephone_zan1);
            }else {
                holder.album_praise_layout.setImageResource(R.mipmap.telephone_zan);

            }
//            Log.e("====jy",list.get(3).getComment().get(1).getName()+"===");
//            点击看大图
            holder.layout_nine_grid.setOnItemImageClickListener(new NineGridLayoutView.OnItemImageClickListener() {
                @Override
                public void onItemImageClick(int position, List<String> img) {
                    Log.e("====",position+"==="+img.get(position)+"===");
                    menuWindow = new SelectPopupWindow(context,img,position);
                    //显示窗口 //设置layout在PopupWindow中显示的位置
                    menuWindow.showAtLocation(holder.album_time, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
                }
            });

            //评论点赞列表UI显示

            if (list.get(position).isCommentup() || list.get(position).isUpcheck()) {
                holder.sanjiao.setVisibility(View.VISIBLE);
                holder.linearlayoutAll.setVisibility(View.VISIBLE);
                //处理点赞列表
                if (list.get(position).isUpcheck()) {
                    if (list.get(position).isCommentup() ) {
                        holder.line.setVisibility(View.VISIBLE);
                    } else {
                        holder.line.setVisibility(View.GONE);
                    }
//                    Log.e("=====jy",list.get(position).isUpcheck()+"++++");
//                    Log.e("=====jy",list.get(position).getUplist().size()+"++++");
                    holder.album_praise_list_layout.setVisibility(View.VISIBLE);
                    holder.praiseListView.setVisibility(View.VISIBLE);
                    holder.parise_icon.setVisibility(View.VISIBLE);
                    Log.e("-----",list.get(position).getUplist().size()+"----");
                    holder.praiseListView.setDatas(list.get(position).getUplist());
                    holder.praiseListView.setOnItemClickListener(new PraiseListView.OnItemClickListener() {
                        @Override
                        public void onClick(int currenr_position) {
                            if( (list.get(position).getUplist().get(currenr_position).getUserid()+"").equals(BaseApplication.BasePreferences.readUID())){
                                context.startActivity(new Intent(context, ContactDetailActivity.class).putExtra("id",(list.get(position).getUplist().get(currenr_position).getUserid()+"")).putExtra("type","0"));
                            }else if((list.get(position).getUplist().get(currenr_position).getUserid()+"").equals(BaseApplication.BasePreferences.readTeacherId())){
                                context.startActivity(new Intent(context, TeacherDetailActivity.class).putExtra("id",(list.get(position).getUplist().get(currenr_position).getUserid()+"")).putExtra("type","1"));

                            }else {
                                context.startActivity(new Intent(context, ContactDetailActivity.class).putExtra("id",(list.get(position).getUplist().get(currenr_position).getUserid()+"")).putExtra("type","1"));

                            }
                        }
                    });
                } else {
                    holder.album_praise_list_layout.setVisibility(View.GONE);
                }
                // 处理评论列表
                if (list.get(position).isCommentup()) {
//                    holder.linearlayoutAll.setVisibility(View.VISIBLE);
//                    holder.linearlayoutAll.setVisibility(View.VISIBLE);
                    if (list.get(position).isUpcheck() ) {
                        holder.line.setVisibility(View.VISIBLE);
                    } else {
                        holder.line.setVisibility(View.GONE);
                    }
                    holder.commentList.setVisibility(View.VISIBLE);

                    holder.commentList.setDatas(list.get(position).getComment());
                } else {

//                    holder.linearlayoutAll.setVisibility(View.GONE);

                }
            } else {
                holder.linearlayoutAll.setVisibility(View.GONE);
                holder.sanjiao.setVisibility(View.GONE);
            }
        }
        setUpItemEvent(holder,position);

    }

    //为RecyclerView添加点击事件
    protected void setUpItemEvent(final FindAdapter.ViewHolder holder, final  int position) {
        final int list_position = position;

        if (onItemClickListener != null) {

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    //  int LayoutPosition = holder.getLayoutPosition();

                    onItemClickListener.onItemClick(holder.itemView, position);



                }

            });

        }
        //点赞
        if(onItemDianzanClickListener != null){
            holder.album_praise_layout_line.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onItemDianzanClickListener.onItemDianzanClick(holder.album_praise_layout, position);
                }
            });
        }
        //主评论
        if(onItemZhuCommentClickListener != null){
            holder.linearlayout_comment_layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onItemZhuCommentClickListener.onItemZhuCommentClick(holder.linearlayout_comment,position);
                }
            });
        }
        //附评论
        if(onItemCommentClickListener != null){
//            holder.commentList.setOnItemClickListener(new CommentListView.OnItemClickListener() {
//                @Override
//                public void onItemClick(int position) {
//                    int comment_posi = position;
//                    onItemCommentClickListener.onItemCommentClick(holder.linearlayout_comment,list_position,comment_posi);
//                }
//            });

            holder.commentList.setOnItemClickListener(new CommentListView.OnItemClickListener() {
                @Override
                public void onItemClick(int commentPosition) {
                    onItemCommentClickListener.onItemCommentClick(holder.linearlayout_comment
                            ,list_position,commentPosition,list.get(position).getComment());
                }
            });
        }

        //昵称点击事件
        holder.album_user_name_txt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if( (list.get(position).getId()+"").equals(BaseApplication.BasePreferences.readUID())){
                    context.startActivity(new Intent(context, ContactDetailActivity.class).putExtra("id",(list.get(position).getId()+"")).putExtra("type","0"));
                }else if((list.get(position).getId()+"").equals(BaseApplication.BasePreferences.readTeacherId())){
                    context.startActivity(new Intent(context, TeacherDetailActivity.class).putExtra("id",(list.get(position).getId()+"")).putExtra("type","1"));

                }else {
                    if(!TextUtils.isEmpty(list.get(position).getName()) || list.get(position).getName() != null){
                        context.startActivity(new Intent(context, ContactDetailActivity.class).putExtra("id",(list.get(position).getId()+"")).putExtra("type","1"));
                    }else {
                        UtilToast.show("此人已经删除账号，不能点击查看他的详情！");
                    }

                }
            }
        });
//        头像点击事件
        holder.album_picture.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                List<String> headList= new ArrayList<String>();
                headList.add(list.get(position).getAvatar());
                selectPopupWindowNoPosition = new SelectPopupWindowNoPosition(context,headList,position);
                //显示窗口 //设置layout在PopupWindow中显示的位置
                selectPopupWindowNoPosition.showAtLocation(holder.album_time, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
            }
        });

        //删除状态点击事件
        if(onItemDelectClickListener != null){
            holder.tv_delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onItemDelectClickListener.onItemDelectClick(holder.tv_delete, position);
                }
            });
        }

    }
    @Override
    public int getItemCount() {
        return list.size();
    }


    //初始化组件
    public static class ViewHolder extends RecyclerView.ViewHolder {

        public TextView album_user_name_txt,album_time,album_content_txt,tv_delete;
        public CircleImageView album_picture;
        public  NineGridLayoutView layout_nine_grid;
        public ImageView album_praise_layout,linearlayout_comment,sanjiao,parise_icon;
        public LinearLayout linearlayoutAll,linearlayout_comment_layout,album_praise_layout_line,album_praise_list_layout;
        public CommentListView commentList;
        public PraiseListView praiseListView;
        public View line;
        public ViewHolder(View itemView) {
            super(itemView);

            album_user_name_txt = (TextView) itemView.findViewById(R.id.album_user_name_txt);
            album_time = (TextView) itemView.findViewById(R.id.album_time);
            album_content_txt = (TextView) itemView.findViewById(R.id.album_content_txt);
            album_picture = itemView.findViewById(R.id.album_picture);
            layout_nine_grid = itemView.findViewById(R.id.layout_nine_grid);
            album_praise_layout = itemView.findViewById(R.id.album_praise_layout);
            linearlayoutAll = itemView.findViewById(R.id.linearlayoutAll);
            commentList = itemView.findViewById(R.id.commentList);
            linearlayout_comment = itemView.findViewById(R.id.linearlayout_comment);
            sanjiao = itemView.findViewById(R.id.sanjiao);
            linearlayout_comment_layout = itemView.findViewById(R.id.linearlayout_comment_layout);
            album_praise_layout_line = itemView.findViewById(R.id.album_praise_layout_line);
            album_praise_list_layout = itemView.findViewById(R.id.album_praise_list_layout);
            praiseListView = itemView.findViewById(R.id.praiseListView);
            line = itemView.findViewById(R.id.line);
            parise_icon = itemView.findViewById(R.id.parise_icon);
            tv_delete = itemView.findViewById(R.id.tv_delete);
        }
    }
}
